<?php

/**
 * material short summary.
 *
 * material description.
 *
 * @version 1.0
 * @author GALAXY
 */
class Material extends Goods_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->Model('Good_material_model');
        $this->load->library('form_validation');
    }

    public function materiallist()
    {
        $cupge=$this->input->get('page');
        $cupge=empty($cupge) || $cupge=="0"?1:$cupge;

        $pagesize=10;
        $one=$this->Good_material_model->get_page($cupge,$pagesize,"*","good_material","order by good_material_id desc","good_material_id","");
        $this->load->library('page');
        $p=new Page();
        $data['materialList']=arrayToObject($one['objlist']);
        $p->init($one['count'][0]->_count,5,$cupge,$pagesize,site_url('admin/material/materiallist'));
        $data['links']=$p->showPages(1);
        $this->load->view('Admin/material/materiallist.html',$data);

    }

    public function addmaterial()
    {
        $this->load->view('Admin/material/addmaterial.html');
    }

    public function add_material()
    {
        $valiRes= $this->form_validation->run('material_vali');
        $this->load->library('ajaxMsgModel');
        $materialModel= new Good_material_model();
        if($valiRes)
        {
            $materialModel->title=$this->input->post('title');

            $res= $materialModel->insert_good_material($materialModel);
            if($res)
            {
                $this->ajaxmsgmodel->Msg='添加成功';
                $this->ajaxmsgmodel->Statu='ok';
                $this->ajaxmsgmodel->BackUrl=site_url("admin/material/materiallist");
            }
            else
            {
                $this->ajaxmsgmodel->Msg='添加失败';
                $this->ajaxmsgmodel->Statu='err';
            }
        }
        else{
            $errors=  $this->form_validation->error_array();
            $this->ajaxmsgmodel->Msg='服务器验证失败:'.get_validate_errors($errors);;
            $this->ajaxmsgmodel->Statu='err';
        }
        echo(json_encode($this->ajaxmsgmodel));
    }

    public function editmaterial()
    {
        $materialId=$this->uri->segment(4);
    	$data['materialModel']=$this->Good_material_model->get_good_material_by_id($materialId);
        $this->load->view('Admin/material/editmaterial.html',$data);
    }

     public function edit_material()
     {
         $valiRes= $this->form_validation->run('material_vali');
         $this->load->library('ajaxMsgModel');
         $materialModel= new Good_material_model();
         if($valiRes)
         {
             $materialModel->good_material_id=$this->input->post('good_material_id');
             $materialModel->title=$this->input->post('title');
             $res= $materialModel->edit_good_material($materialModel);
             if($res)
             {
                 $this->ajaxmsgmodel->Msg='修改成功';
                 $this->ajaxmsgmodel->Statu='ok';
                 $this->ajaxmsgmodel->BackUrl=site_url("admin/material/materiallist");
             }
             else
             {
                 $this->ajaxmsgmodel->Msg='修改失败';
                 $this->ajaxmsgmodel->Statu='err';
             }
         }
         else{
             $errors=  $this->form_validation->error_array();
             $this->ajaxmsgmodel->Msg='服务器验证失败:'.get_validate_errors($errors);;
             $this->ajaxmsgmodel->Statu='err';
         }
         echo(json_encode($this->ajaxmsgmodel));
     }



     public function del_material()
     {
         $this->load->library('ajaxMsgModel');
         $backurl=$this->input->get('returnurl');
         $res=$this->Good_material_model->del_good_material_byid($this->uri->segment(4));
         if($res>0)
         {
             $this->ajaxmsgmodel->Msg='操作成功';
             $this->ajaxmsgmodel->Statu='ok';
             $this->ajaxmsgmodel->BackUrl=$backurl;
         }
         else
         {
             $this->ajaxmsgmodel->Msg='操作失败';
             $this->ajaxmsgmodel->Statu='err';
         }
         echo(json_encode($this->ajaxmsgmodel));
     }

     public function batchdel_material()
     {
         $good_material_id=$this->input->post('items');
         $this->load->library('ajaxMsgModel');
         $backurl=$this->input->get('returnurl');
         $materialModel=new $this->Good_material_model();
         $good_material_idArray=explode('|',trim($good_material_id,'|'));
         $res=0;
         if(count($good_material_idArray)>0)
         {
             $res=$materialModel->batchdel_good_material($good_material_idArray);
         }
         if($res>0)
         {
             $this->ajaxmsgmodel->Msg='操作成功';
             $this->ajaxmsgmodel->Statu='ok';
             $this->ajaxmsgmodel->BackUrl=$backurl;
         }
         else
         {
             $this->ajaxmsgmodel->Msg='操作失败';
             $this->ajaxmsgmodel->Statu='err';
         }
         echo(json_encode($this->ajaxmsgmodel));
     }
}